package casa.depesos.app.com.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import casa.depesos.app.com.App;
import casa.depesos.app.com.R;
import casa.depesos.app.com.activity.MainActivity;
import casa.depesos.app.com.databinding.MeItemBinding;
import casa.depesos.app.com.utils.Constant;


public class MeAdapter extends RecyclerView.Adapter {

    String[] title = App.getApp().getResources().getStringArray(R.array.me_items);
    int[] icon = new int[]{R.mipmap.me_order, R.mipmap.me_bank, R.mipmap.me_card, R.mipmap.me_account, R.mipmap.me_notify, R.mipmap.me_privacy, R.mipmap.me_me, R.mipmap.me_help};
    MainActivity fragment;

    public MeAdapter(MainActivity fragment) {
        this.fragment = fragment;
        if (Constant.white) {
            icon = new int[]{R.mipmap.me_account, R.mipmap.me_privacy, R.mipmap.me_me, R.mipmap.me_help};
            title = App.getApp().getResources().getStringArray(R.array.me_items_white);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        MeItemBinding binding = MeItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return title.length;
    }

    class Holder extends RecyclerView.ViewHolder {
        MeItemBinding binding;

        Holder(MeItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            binding.title.setText(title[p]);
            binding.title.setCompoundDrawablesWithIntrinsicBounds(icon[p], 0, 0, 0);
            itemView.setOnClickListener(v -> {
                int i = getBindingAdapterPosition();
                fragment.jump(i, title[i]);
            });
        }
    }
}
